package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.entity.OrderSubmitParam;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/mobile/order")
public class OrderController {

    //@DubboReference
    //private SmsService smsService;
    @DubboReference
    private OrderService orderService;
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private SetMealService setMealService;
    @DubboReference
    private SmsRocketMQService smsRocketMQService;

    @PostMapping("/submit")
    public Result submit(@RequestParam("type")String type,@RequestBody OrderSubmitParam orderSubmitParam) {
        /*
        * @Date: 5:40 下午 2021/1/10
        * @param: [orderSubmitParam]
        * @return: com.itheima.health.entity.Result
        * @Description: 上传预约信息
        */
        log.info("[预约提交-submit-前端传入数据] - orderSubmitParam: {}",orderSubmitParam);
        //后端验证前端数据
        if (StringUtils.isEmpty(orderSubmitParam.getValidateCode()) || StringUtils.isEmpty(orderSubmitParam.getTelephone())){
            return new Result(false,MessageConst.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        boolean flag = smsRocketMQService.checkValidateCode(orderSubmitParam.getTelephone(), orderSubmitParam.getValidateCode(),type);
        if (flag){
            //组装t_member表
            Member member = new Member();
            member.setName(orderSubmitParam.getName());
            member.setIdCard(orderSubmitParam.getIdCard());
            member.setSex(orderSubmitParam.getSex());
            member.setPhoneNumber(orderSubmitParam.getTelephone());
            memberService.addMember(member);

            Integer orderId = orderService.add(orderSubmitParam);
            Order order = orderService.findById(orderId);
            log.info("[预约提交-submit-后端返回数据] - order: {}",order);

            return new Result(true,MessageConst.ORDERSETTING_SUCCESS,order);
        }
        return new Result(false, MessageConst.VALIDATECODE_ERROR);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        /*
        * @Date: 10:31 上午 2021/1/11
        * @param: [id]
        * @return: com.itheima.health.entity.Result
        * @Description: 返回对应的order信息，没有对应的pojo类，所以使用map进行分=封装
        */
        log.info("[预约提交-findById-前端传入数据] - id: {}",id);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Order order = orderService.findById(id);
        Member member = memberService.findById(order.getMemberId());
        Setmeal setmeal = setMealService.findById(order.getSetmealId());
        Map<String,Object> map = new HashMap<>();
        map.put("member",member.getName());
        map.put("setmeal",setmeal.getName());
        map.put("orderDate",sdf.format(order.getOrderDate()));
        map.put("orderType",order.getOrderType());

        log.info("[预约提交-findById-后端返回数据] - map: {}",map);

        if (order==null||member==null||setmeal==null){
            return new Result(false,MessageConst.QUERY_ORDER_FAIL);
        }
        smsRocketMQService.sendOrderShow(map);
        return new Result(true,MessageConst.ORDER_SUCCESS,map);
    }



}
